package com.ym.jiujiangeyou;

import java.util.List;

import java.util.logging.FileHandler;

import com.ym.jiujiangeyou.adapter.GalleryAdapter;
import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.ui.ImageViewEx;
import com.ym.jiujiangeyou.ui.PicGallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class PictureBrowerActivity extends Activity implements
		OnItemSelectedListener, OnClickListener {
	// 屏幕宽度
	public static int screenWidth;
	// 屏幕高度
	public static int screenHeight;

	private Button mBtnBack;
	private Button mBtnSave;
	private TextView title;
	private PicGallery gallery;
	private MyApplication application = null;
	LinearLayout back_layout;

	private GalleryAdapter mAdapter;

	// private BaseType.InfoItem mItem = new BaseType.InfoItem();
	private int curPos = 0;
	private int totalNum = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		application = MyApplication.getInstance();
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		// log.e("PictureBrowerActivity onCreate");
		setContentView(R.layout.picture_view_activity);
		gallery = (PicGallery) this.findViewById(R.id.pic_gallery);
		initViews();
		initData();
	}

	@SuppressWarnings("deprecation")
	private void initViews() {

		title = (TextView) findViewById(R.id.title_text);
		back_layout = (LinearLayout) findViewById(R.id.title_back);
		screenWidth = getWindow().getWindowManager().getDefaultDisplay()
				.getWidth();
		screenHeight = getWindow().getWindowManager().getDefaultDisplay()
				.getHeight();

		gallery.setVerticalFadingEdgeEnabled(false);// 取消竖直渐变边框
		gallery.setHorizontalFadingEdgeEnabled(false);// 取消水平渐变边框
		gallery.setDetector(new GestureDetector(this, new MySimpleGesture()));

		back_layout.setOnClickListener(this);
		// mBtnBack.setOnClickListener(this);
		// mBtnSave.setOnClickListener(this);

	}

	private void initData() {
		// i ContentCache mContentCache = ContentCache.getInstance();
		// mItem = mContentCache.getInfoItem();
		List<String> list = application.getUrls();
		// log.e("mItem.mImageUrlList.size = " + mItem.mImageUrlList.size());
		totalNum = list.size();
		mAdapter = new GalleryAdapter(this);
		gallery.setAdapter(mAdapter);
		gallery.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				return false;
			}
		});

		mAdapter.setData(list);

		gallery.setOnItemSelectedListener(this);

		updateTitle(0);
	}

	private void updateTitle(int pos) {
		title.setText(String.valueOf(pos + 1) + "/" + String.valueOf(totalNum));
	}

	private class MySimpleGesture extends SimpleOnGestureListener {
		// 按两下的第二下Touch down时触发
		public boolean onDoubleTap(MotionEvent e) {

			View view = gallery.getSelectedView();
			if (view instanceof ImageViewEx) {
				ImageViewEx imageView = (ImageViewEx) view;
				if (imageView.getScale() > imageView.getMiniZoom()) {
					imageView.zoomTo(imageView.getMiniZoom());
				} else {
					imageView.zoomTo(imageView.getMaxZoom());
				}

			} else {

			}
			return true;
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// Logger.LOG("onSingleTapConfirmed",
			// "onSingleTapConfirmed excute");
			// mTweetShow = !mTweetShow;
			// tweetLayout.setVisibility(mTweetShow ? View.VISIBLE
			// : View.INVISIBLE);
			return true;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View view, int pos,
			long arg3) {

		// log.e("onItemSelected pos = " + pos);

		curPos = pos;

		ImageViewEx imageViewEx = (ImageViewEx) view;
		boolean isDefaultBitmap = imageViewEx.getDefaultBitmapFlag();
		if (isDefaultBitmap) {
			mAdapter.syncRefreshImageViewEx(imageViewEx);
		}

		updateTitle(curPos);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.btn_right:
			// save();
			break;
		}
	}

	/*
	 * private void save(){ LAroundApplication.getInstance().onEvent("SAVE01");
	 * boolean isSDCard = CommonUtil.hasSDCard(); if (!isSDCard){
	 * CommonUtil.showToast(R.string.toast_save_fail, this); return ; }
	 * 
	 * if (mCurPos >= mItem.mImageUrlList.size()){
	 * 
	 * return ; }
	 * 
	 * if (!FileHelper.createDirectory(FileManager.getDownloadFileSavePath())){
	 * 
	 * return ; }
	 * 
	 * 
	 * FileCache fileCache = new FileCache(this); String url =
	 * mItem.mImageUrlList.get(mCurPos); String fromPath =
	 * fileCache.getSavePath(url);
	 * 
	 * 
	 * String toPath = FileManager.getDownloadFileSavePath() +
	 * String.valueOf(url.hashCode()); boolean ret =
	 * FileHelper.saveBitmap(fromPath, toPath); if (ret){ String text =
	 * getResources().getString(R.string.toast_save_success) + "," +
	 * getResources().getString(R.string.toast_savefile_end) + toPath + ".jpg";
	 * 
	 * CommonUtil.showToast(text, this); }else{
	 * CommonUtil.showToast(R.string.toast_save_fail2, this); }
	 * 
	 * }
	 */
}